<template>
  <global-layout>
    <contextmenu class="top-nav" :itemList="menuItemList" :visible.sync="menuVisible" @select="onMenuSelect"/>
    <div id="div1">
      <a-tabs @contextmenu.native="e => onContextmenu(e)" v-show="multipage && !collapsed" :active-key="activePage"
              class="tab-layout-tabs " style="height:40px;background:transparent;border:none;padding:0;" :hide-add="true"
              type="editable-card" @change="changePage" @edit="editPage">
        <a-tab-pane class="nav-item" :id="page.fullPath" :key="page.fullPath" v-for="page in pageList" v-if="!page.isShow">
          <span slot="tab" :pagekey="page.fullPath">{{ page.meta.title}}</span>
        </a-tab-pane>
      </a-tabs>
    </div>
    <!-- <div style="margin: 80px 0px 10px 10px;"> -->
    <!-- <div class="top-arrow" style="float: right"><a-icon @click.native="toggle" style="font-size: 28px" :type="!collapsed ? 'fullscreen' : 'fullscreen-exit'"></a-icon></div> -->
    <!--         <a-tooltip placement="left">
          <template slot='title'>
            {{!collapsed ? '全屏': '退出全屏'}}
          </template>
          <a-icon @click.native="toggle" style="font-size: 28px" :type="!collapsed ? 'fullscreen' : 'fullscreen-exit'"></a-icon>
        </a-tooltip> -->
    <!-- </div> -->
    <transition name="page-toggle">
      <div>
        <router-view v-show="!this.$route.meta.isIFrame"/>
        <IframePagePageView v-show="this.$route.meta.isIFrame" @close="remove"/>
      </div>
    </transition>
  </global-layout>
</template>
<script>

import Vue from 'vue'
import GlobalLayout from '@/components/page/GlobalLayout'
import IframePagePageView from '@/components/layouts/IframePageView'
import Contextmenu from '@/components/menu/Contextmenu'
import { INFORMATION,CENTER,COMMISSION } from "@/store/mutation-types"
import { mixin, mixinDevice } from '@/utils/mixin.js'
import { topNavScrollToSelectItem, triggerWindowResizeEvent } from '@/utils/util'
import { mapActions } from 'vuex'
const indexKey = '/home/index'
const basePath = ['/home/index','/home/information','/home/center','/home/commission']
const initPathList = [{
  name: 'home-index',
  path: indexKey,
  fullPath: indexKey,
  meta: {title: '首页',icon: 'dashboard'}
},{
  name: 'home-information',
  path: '/home/information',
  fullPath:'/home/information',
  meta: { title: '局内信息', icon: 'home' }
},{
  name: 'home-center',
  path: '/home/center',
  fullPath:'/home/center',
  meta: { title: '信息中心', icon: 'home' }
},{
  name: 'home-commission',
  path: '/home/commission',
  fullPath:'/home/commission',
  meta: { title: '市规委会', icon: 'home' }
}]
export default {
  name: 'TabLayout',
  components: {
    GlobalLayout,
    IframePagePageView,
    Contextmenu
  },
  mixins: [mixin, mixinDevice],
  data() {
    return {
      informationIsShow:Vue.ls.get(INFORMATION),
      centerIsShow:Vue.ls.get(CENTER),
      commissionIsShow:Vue.ls.get(COMMISSION),
      collapsed: false,
      pageList: [],
      linkList: [],
      activePage:indexKey,
      stablePage:indexKey,
      menuVisible: false,
      menuItemList: [
        { key: '1', icon: 'arrow-left', text: '关闭左侧' },
        { key: '2', icon: 'arrow-right', text: '关闭右侧' },
        { key: '3', icon: 'close', text: '关闭其它' }
      ]
    }
  },
  computed: {
    multipage() {
      //判断如果是手机模式，自动切换为单页面模式
      if (this.isMobile()) {
        return false
      } else {
        return this.$store.state.app.multipage
      }
    }
  },
  created() {
    initPathList[1].isShow = this.informationIsShow;
    initPathList[2].isShow = this.centerIsShow;
    initPathList[3].isShow = this.commissionIsShow;
    initPathList.forEach(item=>{
      this.pageList.push(item);
      this.linkList.push(item.fullPath)
    });
    if(basePath.indexOf(this.$route.path) == -1) {
      this.pageList.push(this.$route)
      this.linkList.push(this.$route.fullPath)
    }
    this.activePage = this.$route.fullPath
  },
  watch: {
    sidebarOpened(val) {
      this.collapsed = !val
    },
    '$route': function(newRoute) {
      this.activePage = newRoute.fullPath
      if(basePath.indexOf(this.activePage)!= -1){
        this.stablePage = this.activePage
      }
      if (!this.multipage) {
        this.linkList = [newRoute.fullPath]
        this.pageList = [newRoute]
      } else if (this.linkList.indexOf(newRoute.fullPath) < 0) {
        this.linkList.push(newRoute.fullPath)
        this.pageList.push(newRoute)
      } else if (this.linkList.indexOf(newRoute.fullPath) >= 0) {
        let oldIndex = this.linkList.indexOf(newRoute.fullPath)
        this.pageList.splice(oldIndex, 1, newRoute)
      }
    },
    'activePage': function(key) {
      let index = this.linkList.lastIndexOf(key)
      var waitRouter = this.pageList[index]
      this.$router.push({
        path: waitRouter.path,
        name: waitRouter.name,
        params: waitRouter.params
      })
      this.setActivePage(key);
    },
    'multipage': function(newVal) {
      if (!newVal) {
        this.linkList = [this.$route.fullPath]
        this.pageList = [this.$route]
      }
    },
    'pageList':function (newPageList) {
      let pathPage = [];
      if (newPageList && newPageList.length>0) {
        for(let i=0;i<newPageList.length;i++){
          let item = newPageList[i];
          if(item.meta.url){
            pathPage.push(item)
          }
        }
      }
      this.setPathPageList(pathPage);
    }
  },
  methods: {
    ...mapActions(['setSidebar','setPathPageList','setActivePage']),
    toggle() {
      this.collapsed = !this.collapsed
      this.setSidebar(!this.collapsed)
      triggerWindowResizeEvent()
    },
    changePage(key) {
      this.activePage = key
      // 只有当前模式是顶部菜单时才执行定位
      if (this.layoutMode === 'topmenu') {
        setTimeout(() => {
          topNavScrollToSelectItem(document)
        }, 100)
      }
    },
    editPage(key, action) {
      this[action](key)
    },
    remove(key) {
      if(basePath.indexOf(key)!= -1){
        if (key == indexKey) {
          this.$message.warning('首页不能关闭!')
        }else if(key == '/home/information'){
          this.$message.warning('局内信息不能关闭!')
        }else if(key == '/home/center'){
          this.$message.warning('信息中心不能关闭!')
        }else if(key == '/home/commission'){
          this.$message.warning('市规委会不能关闭!')
        }
        return
      }
      if (this.pageList.length === 1) {
        this.$message.warning('这是最后一页，不能再关闭了啦')
        return
      }
      this.pageList = this.pageList.filter(item => item.fullPath !== key)
      let index = this.linkList.indexOf(key)
      this.linkList = this.linkList.filter(item => item !== key)
      // index = index >= this.linkList.length ? this.linkList.length - 1 : index
      // this.activePage = this.linkList[index]
      if(this.activePage == key){
        index = index >= this.linkList.length ? this.linkList.length - 1 : index
        this.activePage = this.linkList[index]
      }
      if(this.linkList.length==4){
        this.activePage = this.stablePage
      }
    },
    onContextmenu(e) {
      const pagekey = this.getPageKey(e.target)
      if (pagekey !== null) {
        e.preventDefault()
        this.menuVisible = true
      }
    },
    getPageKey(target, depth) {
      depth = depth || 0
      if (depth > 2) {
        return null
      }
      let pageKey = target.getAttribute('pagekey')
      pageKey = pageKey || (target.previousElementSibling ? target.previousElementSibling.getAttribute('pagekey') : null)
      return pageKey || (target.firstElementChild ? this.getPageKey(target.firstElementChild, ++depth) : null)
    },
    onMenuSelect(key, target) {
      let pageKey = this.getPageKey(target)
      switch (key) {
        case '1':
          this.closeLeft(pageKey)
          break
        case '2':
          this.closeRight(pageKey)
          break
        case '3':
          this.closeOthers(pageKey)
          break
        default:
          break
      }
    },
    closeOthers(pageKey) {
      if(basePath.indexOf(pageKey)!=-1){
        this.linkList = this.linkList.slice(0, 4)
        this.pageList = this.pageList.slice(0, 4)
        if(basePath.indexOf(this.activePage)==-1) {
          this.activePage = this.stablePage
        }
      }else{
        let index = this.linkList.indexOf(pageKey)
        this.linkList = this.linkList.slice(index, index + 1)
        this.pageList = this.pageList.slice(index, index + 1)
        this.linkList.unshift(basePath[0],basePath[1],basePath[2],basePath[3])
        this.pageList.unshift(initPathList[0],initPathList[1],initPathList[2],initPathList[3])
        this.activePage = this.stablePage
      }
      // let index = this.linkList.indexOf(pageKey)
      // if (pageKey == indexKey) {
      //   this.linkList = this.linkList.slice(index, index + 1)
      //   this.pageList = this.pageList.slice(index, index + 1)
      //   this.activePage = this.linkList[0]
      // } else {
      //   let indexContent = this.pageList.slice(0, 1)[0]
      //   this.linkList = this.linkList.slice(index, index + 1)
      //   this.pageList = this.pageList.slice(index, index + 1)
      //   this.linkList.unshift(indexKey)
      //   this.pageList.unshift(indexContent)
      //   this.activePage = this.linkList[1]
      // }
    },
    closeLeft(pageKey) {
      if(basePath.indexOf(pageKey)!= -1){
        this.$message.warning('基础导航不能关闭!')
        return
      }
      let index = this.linkList.indexOf(pageKey)
      this.linkList = this.linkList.slice(index)
      this.pageList = this.pageList.slice(index)
      this.linkList.unshift(basePath[0],basePath[1],basePath[2],basePath[3])
      this.pageList.unshift(initPathList[0],initPathList[1],initPathList[2],initPathList[3])
      if (this.linkList.indexOf(this.activePage) < 0) {
        this.activePage = this.stablePage
      }
    },
    closeRight(pageKey) {
      if(basePath.indexOf(pageKey)!= -1){
        this.linkList = this.linkList.slice(0, 4)
        this.pageList = this.pageList.slice(0, 4)
        if(basePath.indexOf(this.activePage)==-1) {
          this.activePage = this.stablePage
        }
      }else{
        let index = this.linkList.indexOf(pageKey)
        this.linkList = this.linkList.slice(0, index + 1)
        this.pageList = this.pageList.slice(0, index + 1)
        if (this.linkList.indexOf(this.activePage < 0)) {
          this.activePage = this.linkList[this.linkList.length - 1]
        }
      }
    },
  }
}
</script>

<style lang="scss">
/*
 * The following styles are auto-applied to elements with
 * transition="page-transition" when their visibility is toggled
 * by Vue.js.
 *
 * You can easily play with the page transition by editing
 * these styles.
 */
#div1{
  height:33px;
  background:rgba(247,248,250,1);
  box-shadow:0px 4px 10px rgba(0,0,0,0.05);
  padding: 0px 0px 0px 20px;
  /*padding: 5px 0px 8px 30px;*/

}
.page-transition-enter {
  opacity: 0;
}

.page-transition-leave-active {
  opacity: 0;
}

.page-transition-enter .page-transition-container,
.page-transition-leave-active .page-transition-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

/*美化弹出Tab样式*/
.ant-tabs-nav-container {
  margin-top: 1px;
}

/* 修改 ant-tabs 样式 */
.tab-layout-tabs.ant-tabs {
  border-bottom: 1px solid #ccc;
  border-left: 1px solid #ccc;
  background-color: white;
  padding: 0 10px;

  .ant-tabs-bar {
    margin: 0!important;
    border: none;
  }

}

.ant-tabs {

  &.ant-tabs-card .ant-tabs-tab {

    padding: 0 24px !important;
    background-color: white !important;
    margin-right: 10px !important;
    border-radius: 5px!important;
    .ant-tabs-close-x {
      width: 12px !important;
      height: 12px !important;
      opacity: 0 !important;
      cursor: pointer !important;
      font-size: 12px !important;
      margin: 0 !important;
      position: absolute;
      top: 36%;
      right: 6px;
    }

    &:hover .ant-tabs-close-x {
      opacity: 1 !important;
    }

  }

}

.ant-tabs.ant-tabs-card>.ant-tabs-bar {
  .ant-tabs-tab {
    border: none !important;
    // box-shadow:0px 3px 6px rgba(0,0,0,0.08);
    // border-bottom: 1px solid transparent !important;
  }

  
   
}


   

#div1 .ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-tab-active{
    color: #fff !important;

background:linear-gradient(90deg,rgba(62,179,255,1) 0%,rgba(67,110,255,1) 100%);
box-shadow:0px 3px 6px 0 rgba(67,110,255,0.32);



  
}
#div1 .ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-tab{
   // height: 38px!important;
   
}

#div1 .ant-tabs-nav div:first-child {
  height: 30px!important;
  line-height:30px!important;
}
#div1 .ant-tabs-nav div:first-child  .ant-tabs-tab{
    box-shadow:0px 3px 6px rgba(0,0,0,0.08);
}

// #div1 .ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-tab div:first-child{
//   height: 30px!important;
//   line-height:30px!important;
// }
#div1 .ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-tab-active .ant-tabs-close-x{
  color:#fff!important;
}

.top-arrow {
  position: relative;
  top: 0;
  z-index: 99;
}

.top-nav {}
</style>


